package org.example.BIO_File_01;

import java.io.DataInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.UUID;

public class Task implements Runnable{

    private Socket socket;

    public Task(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        // 读取数据
        try {
            DataInputStream dataInputStream = new DataInputStream(socket.getInputStream());
            // 获取后缀
            String s = dataInputStream.readUTF();
            // 输出流 输出文件
            FileOutputStream out = new FileOutputStream("D:\\weizhen\\wz\\testdemo\\IODemo\\IODemo\\temp\\"+ UUID.randomUUID().toString() + s);
            byte[] buffer = new byte[1024];
            int len;
            while ((len = dataInputStream.read(buffer)) > 0) {
                // 保存文件
                out.write(buffer,0,len);
            }
            out.close();
            System.out.println("文件保存成功");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
